﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.UserService;
using DragDraw.Util;

namespace DragDraw
{
    public partial class WorldControl : UserControl, IWidgetManager
    {
        private Dictionary<UserControl, IWidgetContainer> m_containers = new Dictionary<UserControl, IWidgetContainer>();

        public List<IWidgetContainer> Containers { get { return m_containers.Values.ToList<IWidgetContainer>(); } }

        private WarnControl m_warnControl = new WarnControl();

        public WorldControl()
        {
            InitializeComponent();
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            App.Current.Host.Content.FullScreenChanged += new EventHandler(Content_Resized);
            WarnDialog.AttachWidget(m_warnControl);
            WarnDialog.Title = "警告";
            ErrorManager.Instance.OnWarnMessage += new EventHandler<ErrorMessageEventArgs>(OnWarnMessage);
            ErrorManager.Instance.OnServiceErrorMessage += new EventHandler<ErrorMessageEventArgs>(OnServiceErrorMessage);
        }

        void OnServiceErrorMessage(object sender, ErrorMessageEventArgs e)
        {
            m_warnControl.WarnText += "\n服务器返回如下错误：\n" + e.Message;
            WarnDialog.Show = true;
        }

        void OnWarnMessage(object sender, ErrorMessageEventArgs e)
        {
            m_warnControl.WarnText = e.Message;
            WarnDialog.Show = true;
        }

        void Content_Resized(object sender, EventArgs e)
        {
            LayoutRoot.Width = App.Current.Host.Content.ActualWidth;
            LayoutRoot.Height = App.Current.Host.Content.ActualHeight;
            TooPanel.Width = LayoutRoot.Width;
        }

        public void AddContainer(int widgetID, UserControl widget)
        {
            IWidgetContainer container = new WidgetControl();
            container.WidgetID = widgetID;
            container.Widget = widget;
            container.Show = true;
            m_containers.Add(widget, container);
            this.SpaceRoot.Children.Add(container as UserControl);
        }

        public void SetContainer(UserControl widget,
            double posX, double posY, double sizeX, double sizeY, string title)
        {
            IWidgetContainer container = m_containers[widget];
            container.Widget = widget;
            container.PosX = posX;
            container.PosY = posY;
            container.SizeX = sizeX;
            container.SizeY = sizeY;
            container.Title = title;
        }

        public void DelContainer(UserControl widget)
        {
            IWidgetContainer container = m_containers[widget];
            this.SpaceRoot.Children.Remove(container as UserControl);
            m_containers.Remove(widget);
        }

        public void ShowModalDialog(UserControl widget, string title)
        {
            ModalDialog.AttachWidget(widget);
            ModalDialog.Title = title;
            ModalDialog.Show = true;
        }

        public void HideModalDialog()
        {
            ModalDialog.Show = false;
        }

        public void HideWarnDialog()
        {
            WarnDialog.Show = false;
        }

        public void SetBackground(UserControl widget)
        {
            BackgroundRoot.Children.Clear();
            if (widget == null)
            {
                try
                {
                    if (string.IsNullOrEmpty(OperationManager.Instance.CurrentSpace.BackgroundImage))
                        throw new Exception();
                    string bk = OperationManager.Instance.CurrentSpace.BackgroundImage;
                    if (bk.Contains(@"http://") == false)
                    {
                        bk = WidgetHelper.MapFileUrl(OperationManager.Instance.CurrentSpace.OwnerUserID, bk);
                    }
                    ImgBG.Source = new BitmapImage(new Uri(
                        bk));
                    BackgroundRoot.Children.Add(ImgBG);
                }
                catch
                {
                    ImgBG.Source = null;
                }
            }
            else
            {
                BackgroundRoot.Children.Add(widget);
            }
        }

        public bool ShowLoadingPanel
        {
            get { return LoadingPanel.Show; }
            set { LoadingPanel.Show = value; }
        }

        public ITaskBar GetToolPanel()
        {
            return TooPanel;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //create dialog
            ModalDialog.Visibility = Visibility.Collapsed;
            WarnDialog.Visibility = Visibility.Collapsed;
        }

        private bool trackingMouseMove = false;
        private Point mousePosition;

        private void SpaceRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            trackingMouseMove = true;
            element.Cursor = Cursors.Hand;
            if (null != element)
            {
                element.CaptureMouse();
            }
        }

        private void SpaceRoot_MouseMove(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;

                element.SetValue(Canvas.TopProperty, (double)(element.GetValue(Canvas.TopProperty)) + deltaV);

                mousePosition = e.GetPosition(null);
            }
        }

        private void SpaceRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();

            element.Cursor = null;

            mousePosition.X = mousePosition.Y = 0;
        }
    }
}
